package popcorn.ticket.job;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import popcorn.ticket.entity.Ticket;
import popcorn.ticket.service.ScheduleService;
import popcorn.ticket.service.TicketService;
import popcorn.ticket.tool.MovieSche;

public class timerJobTest2 extends TimerTask{
	
	// 这里自动判断电影已经放映
	
	@Override
	public void run() {
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");	
	
		TicketService ts = context.getBean(TicketService.class);
		ScheduleService ss = context.getBean(ScheduleService.class);
		// 根据排期编号 获取 影票编号 -- 修改影票状态

		// 当电影放映时间  + 时长    < 现在时间    -- 影票状态修改为已放映
		Date now = new Date();
		Long nowtime = now.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<MovieSche> list = ss.getSchedule();
		for (MovieSche movieSche : list) {
			try {
				Date end = sdf.parse(movieSche.getEnd_date());
				Long endtime = end.getTime();
				if(endtime < nowtime) {
					List<Ticket> listt = ts.selTicketByScheduleid(movieSche.getAuto());
					for (Ticket ticket : listt) {
						Ticket t = new Ticket();
						t.setTicketid(ticket.getTicketid());
						t.setTicketstatus(2);
						ts.updTicketPlayStatus(t);
					}	
				}
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

		context.close();
	}
}
